/**
 * Created by felix on 2017/3/5.
 */
import React from 'react';
import { connect } from 'dva';
import _ from 'lodash';
import { Table, Button } from 'antd';
import Editor from './edit';
import styles from './index.less';
import StatusSwitch from '../../../components/statusSwitch';

function Players({ player, dispatch }) {
    const { data, editorData } = player;
    const columns = [
        {
            title: '姓名',
            dataIndex: 'name',
        },
        {
            title: '用户名',
            dataIndex: 'user_name',
        },
        {
            title: '类型',
            dataIndex: 'player_type_name',
        },
        {
            title: '生日',
            dataIndex: 'birthday',
        },
        {
            title: '注册时间',
            dataIndex: 'create_date',
        },
        {
            title: '状态',
            dataIndex: 'status',
            render: (t) => <StatusSwitch status={t}/>,
        },
        {
            title: '操作',
            render: (t, r) => {
                return (
                    <div>
                        <a onClick={onEditorData.bind(null, r)}>编辑</a>
                    </div>
                );
            },
        },
    ];
    const onEditorData = (data) => {
        dispatch({
            type: 'player/editorVisible',
            payload: { editorData: data },
        });
    };
    const editProps = {
        name: '玩家数据',
        ns: 'player',
        isNew: _.isEmpty(editorData),
        dispatch,
    };
    return (
        <div className={styles.normal}>
            <div className="buttonWrap">
                <Button type="primary" onClick={onEditorData.bind(null, {})}>添加玩家</Button>
            </div>
            <Table
                rowKey="id"
                columns={columns}
                dataSource={data}
            />
            <Editor {...{ ...player, ...editProps}}/>
        </div>
    );
}

function mapStateToProps({ player }) {
    return { player };
}

export default connect(mapStateToProps)(Players);
